package com.woniu.order.controller;


import cn.hutool.json.JSONUtil;
import com.woniu.model.Result;
import com.woniu.order.form.AddOrderDetailForm;
import com.woniu.order.model.OrderDetails;
import com.woniu.order.service.OrderDetailsService;
import com.woniu.order.util.RedisUtil;
import com.woniu.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/orderDetails")
@Api(tags = "订单详情接口")
public class OrderDetailsController {
    @Resource
    private OrderDetailsService orderDetailsService;

    @Resource
    private RedisUtil redisUtil;
    /**
     * 根据订单编号返回订单详情
     * @param orderId
     * @return
     */
    @ApiOperation("根据订单编号返回订单详情")
    @GetMapping("/selectAllOrderDetailByOrderId/{orderId}")
    public Result<List<OrderDetails>> selectAllOrderDetailByOrderId(
            @PathVariable("orderId")
            @ApiParam(name = "orderId",value = "订单编号")
            String orderId){
        List<OrderDetails> list = orderDetailsService.selectAllOrderDetailByOrderId(orderId);
        System.out.println(JSONUtil.parseObj(list));
        return Result.newListInstance(OrderDetails.class).setData(list).setMessage("查询订单详情成功!");
    }

    /**
     * 员工确认订单，新增详情
     * @param form 新增详情
     * @param token
     * @return 返回新增订单详情集合
     */
    @ApiOperation("员工确认订单，新增详情")
    @PostMapping("/employeeQuery")
    public Result<List<OrderDetails>> employeeQuery(@RequestBody AddOrderDetailForm form, @RequestHeader("token") String token) throws Exception {

        String refreshToken = redisUtil.get(token);
        System.out.println(refreshToken);
        Claims claims = JwtUtil.parseToken(refreshToken, JwtUtil.employSecret);
        String accountId = claims.get("accountId").toString();
        Integer userId = Integer.parseInt(accountId);
        List<OrderDetails> orderDetails = orderDetailsService.employeeQuery(form,userId,form.getOrderNum(),form.getImage());
        return Result.newListInstance(OrderDetails.class).setData(orderDetails).setMessage("员工已确认");
    }



}

